001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.visualization;
025    
026    import EVolve.util.predefinedutils.VizInfo;
027    import EVolve.data.*;
028    import EVolve.visualization.VizFactory.VisualizationFactory;
029    import EVolve.exceptions.VizInfoCreateException;
030    import EVolve.Scene;
031    import java.awt.*;
032    import java.util.HashMap;
033    import javax.swing.*;
034    
035    /**
036     * Two-column table.
037     */
038    public class TableViz extends Visualization {
039        private ReferenceDimension leftColumn; // dimension, left column
040        private ValueDimension rightColumn; // dimension, right column
041    
042        private JTable table; // the table
043        private TableVizModel model; // table model
044    
045        private int[] value; // value of the right column
046    
047        /**
048         * Creates a table.
049         */
050        public TableViz() {
051            model = new TableVizModel();
052            table = new JTable(model);
053    
054            JScrollPane scrollPane = new JScrollPane(table);
055            scrollPane.setBackground(Color.white);
056            scrollPane.getViewport().setBackground(Color.white);
057            ((JPanel)panel).add(scrollPane, BorderLayout.CENTER);
058    
059            addPopupTrigger(table);
060            addPopupTrigger(scrollPane.getViewport());
061        }
062    
063        public Dimension[] createDimension() {
064            leftColumn = new ReferenceDimension();
065            rightColumn = new ValueDimension();
066    
067            Dimension[] returnVal = new Dimension[2];
068            returnVal[0] = leftColumn;
069            returnVal[1] = rightColumn;
070    
071            return returnVal;
072        }
073    
074        protected Component createPanel() {
075            JPanel returnVal = new JPanel(new BorderLayout());
076            returnVal.setBackground(Color.white);
077            return returnVal;
078        }
079    
080        protected JPanel createConfigurationPanel() {
081            return null;
082        }
083    
084        protected void updateConfiguration() {
085            model = new TableVizModel();
086            model.setName(leftColumn.getName(), rightColumn.getName());
087            table.setModel(model);
088        }
089    
090        public void preVisualize() {
091            value = new int[leftColumn.getMaxEntityNumber()];
092            for (int i = 0; i < value.length; i++) {
093                value[i] = 0;
094            }
095        }
096    
097        public void receiveElement(Element element) {
098            if (element.isOptional()) return;
099    
100            value[(int)leftColumn.getField(element)] += rightColumn.getField(element);
101        }
102    
103        public void visualize() {
104            leftColumn.addComparator(new ValueComparator("Value", false, value, leftColumn.getEntityName2IntMap()));
105            sort();
106        }
107    
108        public void sort() {
109            String[] sortedName = new String[leftColumn.getEntityNumber()];
110            int[] sortedValue = new int[leftColumn.getEntityNumber()];
111    
112            for (int i = 0; i < leftColumn.getMaxEntityNumber(); i++) {
113                int sortedIndex = leftColumn.getSortedIndex(i);
114                if (sortedIndex != -1) {
115                    sortedName[sortedIndex] = leftColumn.getEntity(sortedIndex).getName();
116                    sortedValue[sortedIndex] = value[i];
117                }
118            }
119    
120            model = new TableVizModel();
121            model.setName(leftColumn.getName(), rightColumn.getName());
122            model.setValue(sortedName, sortedValue);
123            table.setModel(model);
124        }
125    
126        public void makeSelection() {
127            leftColumn.makeSelection(subjectDefinition.getType(),table.getSelectedRows());
128        }
129    
130        public HashMap getCurrentConfigure() {
131            try {
132                HashMap configure = super.getCurrentConfigure();
133    
134                VizInfo vizInfo = new VizInfo();
135    
136                vizInfo.setFactory((VisualizationFactory)configure.get("Factory"));
137                vizInfo.setSubject((ElementDefinition)configure.get("Subject"));
138                String[] dimensionDefs = new String[2];
139                dimensionDefs[0] = leftColumn.getName() ;
140                dimensionDefs[1] = rightColumn.getName() ;
141                configure.put("Dimension",vizInfo.createDimension(dimensionDefs));
142    
143    
144                return configure;
145            } catch (VizInfoCreateException e) {
146                Scene.showErrorMessage(e.getMessage());
147            }
148            return null;
149        }
150    
151        public ReferenceDimension getLinkableDimension(int dim) {
152            return null;
153        }
154    
155        public AutoImage getImage() {
156            return null;
157        }
158    
159        public long getxMax() {
160            return -1;
161        }
162    
163        public void clearMagnifier() {
164            return;
165        }
166    
167        public void setImage(AutoImage newImage) {
168            return;
169        }
170    
171        public JMenuItem[] createSelectionMenuItem() {
172            return null;
173        }
174    
175        public Object clone() {
176            TableViz o = (TableViz)super.clone();
177    
178            o.leftColumn = (ReferenceDimension)leftColumn.clone();
179            o.rightColumn = (ValueDimension)rightColumn.clone();
180            o.dimension[0] = o.leftColumn;
181            o.dimension[1] = o.rightColumn;
182            o.panelConfiguration = o.createConfigurationPanel();
183            o.panel = new JPanel(new BorderLayout());
184            o.panel.setBackground(Color.white);
185            o.createDialog();
186            o.createMenu();
187    
188            if (value!=null) {
189                o.value = new int[value.length];
190                for (int i=0; i<value.length; i++)
191                    o.value[i] = value[i];
192            }
193    
194            o.model = (TableVizModel)model.clone();
195            o.table = new JTable(o.model);
196    
197            JScrollPane scrollPane = new JScrollPane(o.table);
198            scrollPane.setBackground(Color.white);
199            scrollPane.getViewport().setBackground(Color.white);
200            ((JPanel)o.panel).add(scrollPane, BorderLayout.CENTER);
201    
202            o.addPopupTrigger(o.table);
203            o.addPopupTrigger(scrollPane.getViewport());
204            return o;
205        }
206    }